﻿using IdentityServer4.EntityFramework.DbContexts;
using IdentityServer4.EntityFramework.Entities;
using IdentityServer4.EntityFramework.Options;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AuthenticationServer.Data
{
    //public class IdentityServerConfigurationDbContext
    //{
    //}
    public class IdentityServerConfigurationDbContext : ConfigurationDbContext<IdentityServerConfigurationDbContext>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="T:IdentityServer4.EntityFramework.DbContexts.ConfigurationDbContext" /> class.
        /// </summary>
        /// <param name="options">The options.</param>
        /// <param name="storeOptions">The store options.</param>
        /// <exception cref="T:System.ArgumentNullException">storeOptions</exception>
        public IdentityServerConfigurationDbContext(DbContextOptions<IdentityServerConfigurationDbContext> options,
            ConfigurationStoreOptions storeOptions)
            : base(options, storeOptions)
        {

        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            base.OnModelCreating(modelBuilder);

            ClientClaim(modelBuilder);

            ConfigureResources(modelBuilder);
        }

        private static void ClientClaim(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Client>().ToTable(IdentityServerTableNames.Clients);
            modelBuilder.Entity<ClientGrantType>().ToTable(IdentityServerTableNames.ClientGrantTypes);
            modelBuilder.Entity<ClientRedirectUri>().ToTable(IdentityServerTableNames.ClientRedirectUris);
            modelBuilder.Entity<ClientPostLogoutRedirectUri>().ToTable(IdentityServerTableNames.ClientPostLogoutRedirectUris);
            modelBuilder.Entity<ClientScope>().ToTable(IdentityServerTableNames.ClientScopes);
            modelBuilder.Entity<ClientSecret>().ToTable(IdentityServerTableNames.ClientSecrets);
            modelBuilder.Entity<ClientClaim>().ToTable(IdentityServerTableNames.ClientClaims);
            modelBuilder.Entity<ClientIdPRestriction>().ToTable(IdentityServerTableNames.ClientIdPRestrictions);
            modelBuilder.Entity<ClientCorsOrigin>().ToTable(IdentityServerTableNames.ClientCorsOrigins);
            modelBuilder.Entity<ClientProperty>().ToTable(IdentityServerTableNames.ClientProperties);
        }


        private static void ConfigureResources(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<IdentityResource>().ToTable(IdentityServerTableNames.IdentityResources);
            modelBuilder.Entity<IdentityClaim>().ToTable(IdentityServerTableNames.IdentityClaims);
            modelBuilder.Entity<ApiResource>().ToTable(IdentityServerTableNames.ApiResources);
            modelBuilder.Entity<ApiSecret>().ToTable(IdentityServerTableNames.ApiSecrets);
            modelBuilder.Entity<ApiResourceClaim>().ToTable(IdentityServerTableNames.ApiClaims);
            modelBuilder.Entity<ApiScope>().ToTable(IdentityServerTableNames.ApiScopes);
            modelBuilder.Entity<ApiScopeClaim>().ToTable(IdentityServerTableNames.ApiScopeClaims);

        }
    }
}
